package com.java.service.ServiceImpl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.java.config.ConstantPropertiesUtils;
import com.java.model.vo.MsmVo;
import com.java.service.MsmService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public Boolean send(String phone, String code) {
        //阿里云发送短信模板
        try {
            if(StringUtils.isEmpty(phone)){
                return false;
            }
            //整合阿里云的相关参数 id key 秘钥
            DefaultProfile profile = DefaultProfile.
                    getProfile(ConstantPropertiesUtils.REGION_Id,
                            ConstantPropertiesUtils.ACCESS_KEY_ID,
                            ConstantPropertiesUtils.SECRECT);

            IAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            //request.setProtocol(ProtocolType.HTTPS);//用HTTPS需要设置
            request.setMethod(MethodType.POST);
            request.setDomain("dysmsapi.aliyuncs.com");//调用aliyun的接口地址
            request.setVersion("2017-05-25");
            request.setAction("SendSms");
            //我们需要配置的内容
            //手机号
            request.putQueryParameter("PhoneNumbers", phone);
            //签名名称
            request.putQueryParameter("SignName", "我的谷粒在线教育网站");
            //模板code
            request.putQueryParameter("TemplateCode", "SMS_180051135");
            //验证码  使用json格式   {"code":"123456"}
            Map<String,Object> param = new HashMap();
            param.put("code",code);//JSONObject.toJSONString(param) 转成json格式
            request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));//获取到的验证码需要以json传递

            //调用方法进行短信发送
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }


    //订单发送短信业务
    @Override
    public Boolean send(MsmVo msmVo) {
        if(!StringUtils.isEmpty(msmVo.getPhone())){
            this.send(msmVo.getPhone(),msmVo.getParam());
        }
        return null;
    }


    public Boolean send(String phone, Map<String,Object> param) {
        //阿里云发送短信模板
        try {
            if(StringUtils.isEmpty(phone)){
                return false;
            }
            //整合阿里云的相关参数 id key 秘钥
            DefaultProfile profile = DefaultProfile.
                    getProfile(ConstantPropertiesUtils.REGION_Id,
                            ConstantPropertiesUtils.ACCESS_KEY_ID,
                            ConstantPropertiesUtils.SECRECT);

            IAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            //request.setProtocol(ProtocolType.HTTPS);//用HTTPS需要设置
            request.setMethod(MethodType.POST);
            request.setDomain("dysmsapi.aliyuncs.com");//调用aliyun的接口地址
            request.setVersion("2017-05-25");
            request.setAction("SendSms");
            //我们需要配置的内容
            //手机号
            request.putQueryParameter("PhoneNumbers", phone);
            //签名名称
            request.putQueryParameter("SignName", "我的谷粒在线教育网站");
            //模板code
            request.putQueryParameter("TemplateCode", "SMS_180051135");
            //验证码  使用json格式   {"code":"123456"}
            request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));//获取到的验证码需要以json传递
            //调用方法进行短信发送
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}

